#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2020/5/21 下午2:06
# @Author  : Wang xiaofei
# @File    : generate_only_code.py
# @Software: PyCharm
import string

from random import choice
from datetime import datetime


def generate_only_code(prefix='', now_time=None, length=32):
    """
    生成随机字符串
    Args:
        prefix(str): 固定前缀
        now_time(datetime): 指定时间生成
        length(int): 固定长度生成 默认：32
    """
    if now_time is None:
        now_time = datetime.now()

    time_str = now_time.strftime('%Y%m%d%H%M%S')[1:]
    length = length or 32
    random_len = length-len(time_str)-len(prefix)
    random_len = 0 if random_len < 0 else random_len

    suffix = ''.join([choice(string.digits + string.ascii_letters) for i in range(random_len)])
    only_code = prefix + time_str + suffix
    return only_code


if __name__ == '__main__':
    p_number = generate_only_code('echeck_QR:')
    print(p_number)
    # u_numbers = list()

    # for i in range(50000):
    #     p_number = generate_only_code('U')
    #     if p_number in u_numbers:
    #         raise Exception('again')
    #     u_numbers.append(p_number















